package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理页面")
public class SetmealController {

    @Autowired
    public SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> addNewSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐__{}", setmealDTO);

        setmealService.addOneSetmeal(setmealDTO);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<Object> selectSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询__{}", setmealPageQueryDTO);
        PageResult r = setmealService.selectSetmealByPage(setmealPageQueryDTO);

        return Result.success(r);
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> deleteSetmealByIds(@RequestParam List<Integer> ids) {
        log.info("当前要删除套餐——{}", ids);
        setmealService.deleteSetmealByIds(ids);

        return Result.success();

    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealDTO> selectSetmealById(@PathVariable Integer id) {
        log.info("根据id查询套餐——{}", id);
        SetmealDTO setmealDTO = setmealService.selectSetmealById(id);

        return Result.success(setmealDTO);
    }


    @PutMapping
    @ApiOperation("修改套餐")
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐——{}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        log.info("套餐起售停售——{}", status);


        setmealService.updateStatus(status, id);

        return Result.success();
    }

}
